package display.screens {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import gs.TweenLite;
	
	import org.ilovecode.display.FloorButton;
	import org.ilovecode.display.FloorImage;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * Show garage floor.
	 * 
	 * @author Vjekoslav Ratkajec
	 */
	public class GarageScreen extends FloorScreen {
		
		//{ region Stage elements
		
		//User-friendly garage floor title
		public var floorTitleTextField:TextField;
		
		//Garage floor -2 top view graphic
		protected var _floorMinus2:FloorImage;
		//Garage floor -3 top view graphic
		protected var _floorMinus3:FloorImage;
		//Garage floor -4 top view graphic
		protected var _floorMinus4:FloorImage;
		//Garage floor -5 top view graphic
		protected var _floorMinus5:FloorImage;
		
		//Garage floor -2 button switch
		protected var _floor2Button:FloorButton;
		//Garage floor -3 button switch
		protected var _floor3Button:FloorButton;
		//Garage floor -4 button switch
		protected var _floor4Button:FloorButton;
		//Garage floor -5 button switch
		protected var _floor5Button:FloorButton;
		
		//Download PDF button
		public var downloadPDFButton:SimpleButton;
		
		//} endregion
		
		//{ region Variables
		
		//Currently selected floor number
		private var _floorNumber:int = -1;
		//Animation duration
		private var duration:Number = 0.4;
		//Currently displayed garage
		private var currentGarage:FloorImage = null;
		//Titles for garage floors
		private var titles:Array = new Array(
			"TLOCRT PRVE PODZEMNE GARAŽE",
			"TLOCRT DRUGE PODZEMNE GARAŽE",
			"TLOCRT TREĆE PODZEMNE GARAŽE",
			"TLOCRT ČETVRTE PODZEMNE GARAŽE");
			
		private var _contentTextField:TextField;
		
		//} endregion
		
		//{ region Contructor and initialization
		
		/**
		 * Create new instance of Garage screen.
		 */
		public function GarageScreen() {
			super(ScreenName.GARAGE, "garageScreen");
		}
		
		override protected function construct():void
		{
			super.construct();
			
			var xml:XML = controller.getScreenXML("garage");
			for (var i:uint = 2; i < 6; i++) {
				this["_floorMinus" + i] = FloorImage(this.getChildByName("floorMinus" + i));
				FloorImage(this["_floorMinus" + i]).loadImage(XML(xml.floorImages.image.(@id == ("-" + i))).@url);
				
				/*this["_floor" + i + "Button"] = FloorButton(this.getChildByName("floor" + i + "Button"));
				FloorButton(this["_floor" + i + "Button"]).id = -i;
				_floorButtons.push(this["_floor" + i + "Button"]);*/
			}
			
			_contentTextField = TextField(this.getChildByName("contentTextField"));
			_contentTextField.autoSize = TextFieldAutoSize.LEFT;
			_contentTextField.multiline = true;
			_contentTextField.wordWrap = true;
		}
		
		override protected function initText():void
		{
			super.initText();
			
			var xml:XML = controller.getScreenXML("garage");
			_contentTextField.htmlText = String(xml.info).split("\r").join("");
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			for (var i:uint = 2; i < 6; i++) {
				FloorImage(this["floorMinus" + i]).alpha = 0;
			}
			
			initTextFloorButton(-2);
			initTextFloorButton(-3);
			initTextFloorButton(-4);
			initTextFloorButton(-5);
			
			showFloor(2);
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			downloadPDFButton.addEventListener(MouseEvent.CLICK, downloadPDFButton_Click);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			downloadPDFButton.removeEventListener(MouseEvent.CLICK, downloadPDFButton_Click);
		}
		
		
		//} endregion
		
		//{ region Handlers
		
		private function downloadPDFButton_Click(event:MouseEvent):void {
			var url:String = "http://www.ban-centar.hr/content/pdf/garaza/G-" + _floorNumber + ".pdf";
			navigateToURL(new URLRequest(url), "_blank");
		}
		
		//} endregion
		
		//{ region Public
		
		/**
		 * Switch from current to desired floor.
		 * 
		 * @param	floorNumber	Floor number to show.
		 */
		override public function showFloor(floorNumber:int):void {
			floorNumber = Math.abs(floorNumber);
			if (_floorNumber != floorNumber) {
				TweenLite.to(FloorImage(this["floorMinus" + floorNumber]), duration, { alpha:1 } );
				
				if (currentGarage != null) {
					TweenLite.to(currentGarage, duration, { alpha:0 } );
				}
				currentGarage = this["floorMinus" + floorNumber];
				floorTitleTextField.text = String(titles[floorNumber - 2]);
			}
			_floorNumber = floorNumber;
		}
		
		//} endregion
		
	}

}